﻿Imports System.Xml.Serialization
Imports System.Text.RegularExpressions

Public Class TableConfig
    Inherits DefaultConfig
    Private Shared mConfigInfo As New TableInfo
    Private Shared mConfigFilePath As String
    Private Shared mFileDateTime As DateTime

    Public Shared ReadOnly Property ConfigInfo() As TableInfo
        Get
            mConfigInfo = load()
            Return mConfigInfo
        End Get
    End Property

    Shared Sub New()
        mConfigInfo = load()
    End Sub

    Private Shared Function load() As TableInfo
        mErrMessage = Nothing
        If IO.File.Exists(ConfigFilePath) Then
            Try
                Return CType(LoadConfig(mFileDateTime, ConfigFilePath, mConfigInfo), TableInfo)
            Catch ex As Exception
                mErrMessage = ex.Message
                mConfigInfo = New TableInfo
            End Try
        End If
        Return New TableInfo
    End Function

    REM 配置文件路径
    Public Overloads Shared ReadOnly Property ConfigFilePath() As String
        Get
            If String.IsNullOrEmpty(mConfigFilePath) Then
                Dim Path As String = AppSettings("ConfigPath")
                If Not Path.EndsWith("/") Then Path = String.Concat(Path, "/")
                Path = System.Web.HttpContext.Current.Server.MapPath(String.Format("{0}tables.config", Path))
                If IO.File.Exists(Path) Then
                    mConfigFilePath = Path
                Else
                    Path = Nothing
                End If
            End If
            Return mConfigFilePath
        End Get
    End Property
End Class

<Serializable()> <XmlRoot("Tables")> Public Class TableInfo
    Implements IConfigInfo
    Private CurrentName As String
    <XmlElement("Table")> Public Items() As TableItem

    REM 查找数组
    Public Function Find(ByVal Name As String) As TableItem
        CurrentName = Name
        If Items Is Nothing OrElse Items.Length = 0 Then Return Nothing
        Return Array.Find(Items, AddressOf FindWhere)
    End Function

    Private Function FindWhere(ByVal item As TableItem) As Boolean
        Return item.Name = CurrentName
    End Function
End Class

<Serializable()> Public Class TableItem
    Public ID As Integer
    Public Name As String
    Public Xml As String
    Public ACL As String
End Class


